package cn.com.dhcc.ddi.node.controller;

import java.util.Date;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.com.dhcc.app.core.base.BaseController;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.handler.pagerhandler.Pager;
import cn.com.dhcc.app.core.handler.pagerhandler.PagerData;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.core.tag.CommonStaticCode.OPENCLOSE;
import cn.com.dhcc.app.core.util.StrUtil;
import cn.com.dhcc.app.core.util.UUIDGenerator;
import cn.com.dhcc.app.pub.core.tag.staticcode.StaticCode;
import cn.com.dhcc.ddi.node.service.DomainInfoService;
import cn.com.dhcc.ddi.node.vo.DomainInfo;

/**
 * 
    * @ClassName: NodeInfo Controller
    * @Description: 业务域Controller
    * @author 党向阳
    * @date 2016-11-22
    *
 */

@Controller
@RequestMapping("/node")
public class DomainInfoController extends BaseController {
	@Autowired
	private  DomainInfoService  domainInfoService;
	
	private static Logger logger = Log.getLogger(LOGTYPE.DDI);
	/**
	 * 
	    * @Title: list
	    * @Description: 获取列表
	    * @param @param model
	    * @param @param pager
	    * @param @param name
	    * @param @param orderName
	    * @param @param order
	    * @param @return    参数
	    * @return String    返回类型
	    * @throws
	 */
	@RequestMapping(value = "/domain_list")
	public String list(Model model, Pager pager, String name, String orderName, String order) {
		try {
			WhereCondition searchData = new WhereCondition();
			if (StrUtil.isNotBlank(name)) {
				searchData.where().like("name", "%"+name+"%");
			}
			if (StrUtil.isNotBlank(orderName)) {
				searchData.orderBy(orderName + " " + order);
			} else {
				searchData.orderBy("mtime  desc");
			}
			PagerData<DomainInfo> datas = domainInfoService.list(pager, searchData);
			model.addAttribute(DATA_KEY, datas.getDatas());
			setTotoal(model, datas.getTotal());
			return "node/domain/domain_info_list";
		} catch (Exception e) {
			logger.error("操作失败", e);
			return toAlertPage(model, e.getMessage());
		}
	}
	/**
	 * 
	    * @Title: get
	    * @Description: 获取对象
	    * @param @param model
	    * @param @param id
	    * @param @return    参数
	    * @return String    返回类型
	    * @throws
	 */
	@RequestMapping(value = "/domain_get", method = RequestMethod.GET)
	public String get(Model model,String id) {
		try {
			DomainInfo vo = domainInfoService.get(id);
			model.addAttribute(DATA_KEY, vo);
		} catch (Exception e) {
			logger.error("获取业务域信息失败", e);
			return toAlertPage(model, e.getMessage());
		}
		return "node/domain/domain_info_get";
	}
	
	
	/**
	 * 
	    * @Title: update
	    * @Description: 修改
	    * @param @param model
	    * @param @param id
	    * @param @return    参数
	    * @return String    返回类型
	    * @throws
	 */
	
	@RequestMapping("/domain_update")
	public String update(Model model, String id) {
		try {	
			
			DomainInfo domain = domainInfoService.get(id);
			model.addAttribute(VO_KEY, domain);
			return "node/domain/domain_info_update";
		} catch (Exception e) {
			logger.error("操作失败", e);
			return toAlertPage(model, e.getMessage());
		}
	}
	
	@RequestMapping(value = "/update_domain", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> update(Model model, DomainInfo vo) {
		String msg = "";
		try {
			
			vo.setMtime(new Date());
			domainInfoService.update(vo);
			msg = "更新成功";
			return jsonSuccessResult(msg);
		} catch (Exception e) {
			msg = "更新失败";
			logger.error(msg, e);
			return jsonFailResult(msg);
		}
	}
	
	
	/**
	 * 
	    * @Title: addNode
	    * @Description: 添加
	    * @param @param model
	    * @param @return    参数
	    * @return String    返回类型
	    * @throws
	 */
	@RequestMapping("/domain_add")
	public String addNode(Model model) {
		try {
			return "node/domain/domain_info_add";
		} catch (Exception e) {
			logger.error("操作失败", e);
			return toAlertPage(model, e.getMessage());
		}
	}
	
	@RequestMapping(value = "/add_domain", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addNode(Model model, DomainInfo vo) {
		String msg = "";
		try {
			vo.setId(UUIDGenerator.getUUID());
			vo.setMtime(new Date());
			vo.setStatus(OPENCLOSE.OPEN.getStatus());
			domainInfoService.insert(vo);
			msg = "添加成功";
			return jsonSuccessResult(msg);
		} catch (Exception e) {
			msg = "添加失败";
			logger.error(msg, e);
			return jsonFailResult(msg);
		}
	}
	
	/**
	 * 根据ID删除记录
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/domain_delete", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> delete(String id) {
		try {
			//查询业务域ID是否在数据分类中被选中
			if(domainInfoService.listdomainId(id).size()>0){
			return jsonFailResult("删除失败,业务域已关联数据分类,无法删除！");
			}else
			domainInfoService.delete(id);
			return jsonSuccessResult("删除成功");
		} catch (Exception e) {
			logger.error("删除失败", e);
			return jsonFailResult("删除失败");
		}
	}
	
	/**
	 * 
	    * @Title: batchDelete
	    * @Description: 批量删除(备用)
	    * @param @param ids
	    * @param @return    参数
	    * @return Map<String,Object>    返回类型
	    * @throws
	 */
	
	@RequestMapping(value = "/domain_deletes", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> batchDelete(@RequestParam("ids") String[] ids) {
		String msg = "";
		try {
			domainInfoService.batchDelete(ids);
			msg = "批量删除成功";
			return jsonSuccessResult(msg);
		} catch (Exception e) {
			msg = "批量删除失败";
			logger.error(msg, e);
			return jsonFailResult(msg);
		}
	}
	
	
	
	/**
	 * 校验名称是否被使用
	 * @param name
	 * @param oldValue
	 * @return
	 */
	@RequestMapping("/domainInfo_check_name")
	@ResponseBody
	public String isNodeNameUnique(@RequestParam("name") String newValue,@RequestParam("oldValue") String oldValue) {
		try {
			
			if (domainInfoService.isPropertyUnique("name", newValue, oldValue)) {
				return FORM_CHECK_SUCCESS;
			} else {
				return FORM_CHECK_FAIL;
			}
		} catch (Exception e) {
			return FORM_CHECK_FAIL;
		}
	}
	
	/**
	 * 
	    * @Title: lockNodeInfo
	    * @Description: 物理删除
	    * @param @param id
	    * @param @param name
	    * @param @return    参数
	    * @return Map<String,Object>    返回类型
	    * @throws
	 */
	@RequestMapping(value="/lock_domain_info")
	@ResponseBody
	public Map<String, Object> lockDomainInfo(String id, String name){
		String status = StaticCode.DATA_STATUS.DISABLE.getCode();
		boolean isSuc = domainInfoService.del(id, status);
		if (isSuc){
			return jsonSuccessResult("停用成功");
		}
		return jsonFailResult("停用失败");
	}
	
	@RequestMapping(value="/unlock_domain_info")
	@ResponseBody
	public Map<String, Object> unlockDomainInfo(String id, String name){
		String status = StaticCode.DATA_STATUS.ENABLE.getCode();
		boolean isSuc = domainInfoService.del(id, status);
		if (isSuc){
			return jsonSuccessResult("启用成功");
		}
		return jsonFailResult("启用失败");
	}
	
}


